﻿using Decoration.IRepository;
using Decoration.IService;
using Decoration.Models.Dto;
using Decoration.Models.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Decoration.Service
{
    public class ProjectInfoService : BaseService<ProjectInfo>, IProjectInfoService
    {
        private readonly IProjectInfoRepository rep;

        public ProjectInfoService(IProjectInfoRepository rep, IBaseQueryRepository brep) : base(rep, brep)
        {
            this.rep = rep;
        }

        public IEnumerable<ProjectDto> Query(string projectName,int pageSize,int pageIndex,out int total,int? leader=null)
        {
            string sql = @"	select project.*
		                ,business.BusinessName
		                ,completed.DictName as 'CompletedStatusName'
		                from ProjectInfos project
		                inner join BusinessInfos business on project.BusinessType = business.Id
		                inner join vw_dict completed on project.CompletedStatus = completed.DictValue and completed.DictTypeName = '项目完成状态'";

            if (!string.IsNullOrEmpty(projectName)) sql += " and projectName like @projectName";
            if (leader!=null) sql += " and leader = @leader";

            string countSql = $"select count(*) from ({sql}) fff";

            total = rep.ExecuteScalar<int>(countSql, new { projectName = $"%{projectName}%",leader });

            sql += " order by createTime desc";
            sql += " offset @pageSize*(@pageIndex-1) rows fetch next @pageSize rows only";

            return rep.Query<ProjectDto>(sql, new { projectName = $"%{projectName}%",leader, pageSize, pageIndex });
        }
    }
}
